package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        //查看该菜单是否包含菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishMapper.selectCount(dishLambdaQueryWrapper);
        if (dishCount != 0) {
            throw new CustomException("当前分类下关联了菜单,不能删除");
        }
        //查看该套餐是否包含菜品
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, id);
        int setmealCount = setmealMapper.selectCount(lqw);
        if (setmealCount != 0) {
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        //this.removeById(id);
        categoryMapper.deleteById(id);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> seletePage(int page, int pageSize) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getSort);
        Page<Category> categoryPage = new Page<>(page, pageSize);
        categoryMapper.selectPage(categoryPage, lqw);
        return categoryPage;
    }

    /**
     * 新增分类
     *
     * @param category
     */
    @Override
    public void addCategory(Category category) {
        categoryMapper.insert(category);
    }

    /**
     * 修改分类
     *
     * @param category
     */
    @Override
    public void editCategory(Category category) {
        categoryMapper.updateById(category);
    }

    /**
     * 根据分类类型,查询分类数据
     *
     * @param category
     * @return
     */
    @Override
    public List<Category> getCategoryList(Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(category.getType() != null, Category::getType, category.getType());
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> categoryList = categoryMapper.selectList(lqw);
        return categoryList;
    }

}

